<?php
include('functions.inc.php');
$link = db_connect();

$article_query = sprintf("SELECT state, link, title FROM articles WHERE id = %s",
	mysql_real_escape_string($_GET['id'])
);

$result = mysql_query($article_query, $link)
	or die("Can't load article: " . mysql_error() . "\n");

while ($article = mysql_fetch_array($result)) {
	$states = explode(',',$article['state']);
	if (sizeof($states) == 1 && $states[0] == '') {
		$states = array();
	}

	$newState = mysql_real_escape_string($_GET['state']);
	if ($_GET['action'] == 'add') {
		if (array_search($newState,$states) === false) {
			$states[] = $newState;
		}
	} else if ($_GET['action'] == 'remove') {
		$newStates = array();
		foreach ($states as $state) {
			if ($state != $newState) {
				$newStates[] = $state;
			}
		}
		$states = $newStates;
	}

	$states = implode($states,',');

	$update_query = sprintf("UPDATE articles SET state = %s WHERE id = %s",
		$states == '' ? "null" : "'$states'",
		mysql_real_escape_string($_GET['id'])
	);

	mysql_query($update_query, $link)
		or die("Can't load article: " . mysql_error() . "\n");

	if ($_GET['action'] == 'add') {
		if ($_GET['state'] == 'starred') {
			?>
			$('starAction').innerHTML = 'starred';
			$('starAction').className = 'actionDone';
			$('starAction').onclick = function() {action('starred','remove',<?php echo $_GET['id'] ?>);}
			<?php
			if (isset($config['ma.gnolia.key']) && $config['ma.gnolia.key'] != null) {
				// Add starred items to ma.gnolia
				$magnolia_result = magnolia_add($article['title'], $article['link']);
				$attr = $magnolia_result->response->attributes();
				if ($attr["status"] != 'ok') {
					$attr = $magnolia_result->response->error->attributes();
					?>alert('Error while publishing to ma.gnolia: [<?php print $attr["code"] . '] ' . $attr["message"] ?>');
					/**
					 ** Response from ma.gnolia API:
					<?php var_dump($magnolia_result) ?>
					 **/
					<?php
				}
			}
		} else if ($_GET['state'] == 'unread') {
			?>
			$('unreadAction').innerHTML = 'unread';
			$('unreadAction').className = 'actionDone';
			$('unreadAction').onclick = function() {action('unread','remove',<?php echo $_GET['id'] ?>);}
			<?php
		} else if ($_GET['state'] == 'open') {
			// Nothing to do; echo any JS we want to execute here...
		}
	} else if ($_GET['action'] == 'remove') {
		if ($_GET['state'] == 'starred') {
			?>
			$('starAction').innerHTML = 'star this!';
			$('starAction').className = 'action';
			$('starAction').onclick = function() {action('starred','add',<?php echo $_GET['id'] ?>);}
			<?php
			if (isset($config['ma.gnolia.key']) && $config['ma.gnolia.key'] != null) {
				// Remove starred items from ma.gnolia
				$magnolia_result = magnolia_remove($article['link']);
				$attr = $magnolia_result->response->attributes();
				if ($attr["status"] != 'ok') {
					$attr = $magnolia_result->response->error->attributes();
					?>alert('Error while removing item from ma.gnolia: [<?php print $attr["code"] . '] ' . $attr["message"] ?>');
					/**
					 ** Response from ma.gnolia API:
					<?php var_dump($magnolia_result) ?>
					 **/
					<?php
				}
			}
		} else if ($_GET['state'] == 'unread') {
			?>
			$('unreadAction').innerHTML = 'keep this unread';
			$('unreadAction').className = 'action';
			$('unreadAction').onclick = function() {action('unread','add',<?php echo $_GET['id'] ?>);}
			<?php
		}
	}
}

mysql_close($link);
?>